package com.cmxh.shipper.controller;

/*
 *
 *钱包表Controller
 * */

import com.cmxh.shipper.pojo.Wallet;
import com.cmxh.shipper.pojo.WalletRechargeRecord;
import com.cmxh.shipper.service.WalletRechargeRecordService;
import com.cmxh.shipper.service.WalletService;
import com.cmxh.shipper.util.Code;
import com.cmxh.shipper.util.Md5Util;
import com.cmxh.shipper.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Walletcontroller {
    @Autowired
    private WalletService walletService;
    @Autowired
    private WalletRechargeRecordService walletRechargeRecordService;

    //查询钱包
    @RequestMapping("/getWallet/{fid}")
    public Result getWallet(@PathVariable("fid") Integer fid) {
        Wallet wallet = walletService.getWallet(fid);
        if (wallet != null) {
            return new Result(Code.GET_OK, wallet, "查询成功");
        } else {
            return new Result(Code.GET_ERR, "查询失败");
        }
    }

    //修改钱包余额
    @Transactional//事务控制
    @RequestMapping("/updatefid")
    public Result updatefid(@RequestBody Wallet wallet) {
        Wallet wallet1 = walletService.getWallet(wallet.getFid());
        if (wallet1 == null) {
            return new Result(Code.GET_ERR, "钱包不存在");
        }else {
            int updatefid = walletService.updatefid(wallet);
            if (updatefid > 0){
                WalletRechargeRecord walletRechargeRecord = new WalletRechargeRecord();
                walletRechargeRecord.setWalletid(wallet1.getWalletid());
                walletRechargeRecord.setAmount(wallet.getBalance());
                walletRechargeRecord.setStatus(0);
                int insert = walletRechargeRecordService.insert(walletRechargeRecord);
                if (insert > 0) {
                    return new Result(Code.GET_OK, "添加成功");
                } else {
                    return new Result(Code.GET_ERR, "添加失败");
                }
            }else {
                return new Result(Code.GET_ERR, "查询失败");
            }

        }
    }
    @Transactional//事务控制
    @RequestMapping("/updatefid1")
    public Result updatefid1(@RequestBody Wallet wallet) {
        Wallet wallet1 = walletService.getWallet(wallet.getFid());
        if (wallet1 == null) {
            return new Result(Code.GET_ERR, "钱包不存在");
        }else{
            int updatefid = walletService.updatefid(wallet);
            if (updatefid>0){
                WalletRechargeRecord walletRechargeRecord = new WalletRechargeRecord();
                walletRechargeRecord.setWalletid(wallet1.getWalletid());
                walletRechargeRecord.setAmount(wallet.getBalance());
                walletRechargeRecord.setStatus(1);
                int insert = walletRechargeRecordService.insert(walletRechargeRecord);
                if (insert>0){
                    return new Result(Code.GET_OK, "添加成功");
                }else {
                    return new Result(Code.GET_ERR, "添加失败");
                }
            }else {
                return new Result(Code.GET_ERR, "查询失败");
            }
        }
    }

    //新增钱包
    @RequestMapping("/insertWallet/{fid}")
    public Result insertWallet(@PathVariable Integer fid) {
        Wallet wallet1 = walletService.getWallet(fid);
        if (wallet1 != null) {
            return new Result(Code.GET_ERR, "钱包已存在");
        } else {
            int insert = walletService.insert(fid);
            if (insert > 0) {
                return new Result(Code.GET_OK, "添加成功");
            } else {
                return new Result(Code.GET_ERR, "添加失败");
            }
        }
    }
    @RequestMapping("/xiugai")
    public Result xiugai(@RequestBody Wallet wallet) {

        int updatefid = walletService.xiugai(wallet);
        if (updatefid > 0) {
            return new Result(Code.GET_OK, "修改成功");
        } else {
            return new Result(Code.GET_ERR, "修改失败");
        }
    }
    @RequestMapping("/panduan")
    public Result panduan(@RequestBody Wallet wallet) {
        Wallet wallet1 = walletService.getWallet(wallet.getFid());
        String encrypt = Md5Util.encrypt(wallet.getPaymentPassword());
        if (wallet1.getPaymentPassword().equals(encrypt)) {
            return new Result(Code.GET_OK, wallet1, "查询成功");
        } else {
            return new Result(Code.GET_ERR, "查询失败");
        }
    }


}